package com.npjscg.medicalpro.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.common.Constants;
import com.npjscg.medicalpro.common.Result;
import com.npjscg.medicalpro.controller.dto.TemplateDTO;
import com.npjscg.medicalpro.entity.Template;
import com.npjscg.medicalpro.entity.User;
import com.npjscg.medicalpro.service.ITemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author raote
 * @since 2022-06-28
 */
@Api(tags = "报表管理模块")
@RestController
@RequestMapping("/template")
public class TemplateController {

    @Autowired
    private ITemplateService templateService;

    //新建模板
    @ApiOperation(value = "新建模板")
    @PostMapping("/newTemplate")
    public Result newTemplate(@RequestBody Template template){
        Integer adminid = template.getAdminid();
        String temName = template.getTemName();
        if (adminid == null || StrUtil.isBlank(temName)){
            System.out.println(template);
            return Result.error(Constants.CODE_400, "参数错误");
        }
        else {
            templateService.newTemplate(template);
            return Result.success();
        }
    }



/*    分页查询
    查询所有管理员名下的模板：
    需要传入当前页号、当前页面需要多少条记录，管理员id，可选模板名字进行模糊查询
    */
    @ApiOperation(value = "分页查询，仅查询当前管理员名下的模板")
    @GetMapping(value = {"/findAll/{pagenum}/{pagesize}/{adminid}/{temname}", "/findAll/{pagenum}/{pagesize}/{adminid}"})
    public Result findAll(@PathVariable int pagenum, @PathVariable int pagesize, @PathVariable int adminid ,@PathVariable(required = false) String temname){
        //设置起始值及每页条数
        if (temname == null)
            temname = "";
        Page<Template> page = new Page<>(pagenum,pagesize);
        return Result.success(templateService.selectPage(page, adminid, temname));
    }


    //提供给用户查询全部没有填写过的模板
    @ApiOperation(value = "提供给用户查询全部没有填写过的模板")
    @GetMapping("/findAllForU")
    public Result findAllForU(@RequestParam("pagenum") int pagenum, @RequestParam("pagesize") int pagesize ,@RequestParam(("uid")) int uid, @RequestParam(value = "temname", defaultValue = "",required = false) String temname){
        //设置起始值及每页条数
        if (temname == null)
            temname = "";
        //转成SQL语句可用字符
        temname = "%" + temname +"%";
        Page<Template> page = new Page<>(pagenum,pagesize);
        return Result.success(templateService.selectAll(page, uid, temname));
    }



    //通过id来删除模板
    @ApiOperation(value = "通过id来删除模板")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable int id){
        if (id < 0 )
            return Result.error(Constants.CODE_400,"id提供错误");

        templateService.delete(id);
        return Result.success();
    }


    //提供id来渲染前端报表模板
    @ApiOperation(value = "通过提供id来渲染前端报表模板")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable int id){
        if (id < 0 )
            return Result.error(Constants.CODE_400,"id提供错误");
        Template byId = templateService.getById(id);
        if (byId == null){
            System.out.println(id);
            System.out.println(byId);
            return Result.error(Constants.CODE_600, "数据库中没有该模板");
        }
        return Result.success(byId);
    }


    //获取完成率
    @GetMapping("/getRate/{id}")
    public Result getRate(@PathVariable int id){
        if (id < 0 )
            return Result.error(Constants.CODE_400,"id提供错误");
        Template byId = templateService.getById(id);
        if (byId == null)
            return Result.error(Constants.CODE_600, "数据库中没有该模板");
        return Result.success(templateService.getRateList(id));
    }
}
